var args = {pageId : 1, pageSize : 10};
var param = {reqData : args, reqUrl : "queryTicket"};


$(function()
{
	$("#leftcolumn ul li").css("background-color","white");
	$("#myTicket").css("background-color","#ccffff");
	$("#divTable").hide();
	$("#changePwd").hide();
	$("#money").hide();
	$("#edit_data").hide();
	$("#main_middle").show();
	$("#ka").html("&nbsp;&gt;&nbsp;我的订单");
	queryTicket("first");

	$("#user").css("background-color","#ccffff");
	
});


function myTicket(){
	$("#leftcolumn ul li").css("background-color","white");
	$("#myTicket").css("background-color","#ccffff");
	$("#divTable").hide();
	$("#changePwd").hide();
	$("#money").hide();
	$("#edit_data").hide();
	$("#main_middle").show();
	$("#ka").html("&nbsp;&gt;&nbsp;我的订单");
	queryTicket("first");
	
}
queryTicket = function(type)
{
	ajaxRequest(param, function(returnData)
	{
		if(returnData)
		{
			var page = returnData.page;
			args.pageCount = page.pageCount;
			args.dataCount = page.dataCount;
			
			var dataArr = returnData.data;
			
			showData(dataArr);
			
			if(type == "first")
			{
				setSelectedCss($("#divPage a:eq(0)"));
			}
			else if(type == "pre")
			{
				setSelectedCss($("#divPage a:eq(1)"));
			}
			else if(type == "next")
			{
				setSelectedCss($("#divPage a:eq(2)"));
			}
			else
			{
				setSelectedCss($("#divPage a:eq(3)"));
			}
		}
	}, null);
}

showData = function(dataArr)
{
	$("table tr:gt(0)").empty();
	
	var dataCount = parseInt(args.dataCount);
	var pageId = parseInt(args.pageId);
	
	if(dataCount == 0)
	{
		$("#divMsg").css("height","250px");
		return;
	}
	var trs = [];
	
	$(dataArr).each(function()
	{
		var ticketId = this.ticketId;

		var ticketCode = this.ticketCode;
		var movieName = this.movieName;
		var editionName = this.editionName;
		
		var playTime1 = this.playTime1;
		var playTime1Arr = playTime1.match(/\d+/g);
		playTime1 = playTime1Arr[0] + "年" + playTime1Arr[1] + "月" + playTime1Arr[2] + "日 " + playTime1Arr[3] + "时" + playTime1Arr[4] + "分";
		
		var ticketSeat = this.ticketSeat + "号";
		
		var ticketDate = this.ticketDate;
		var ticketDateArr = ticketDate.match(/\d+/g);
		ticketDate = ticketDateArr[0] + "年" + ticketDateArr[1] + "月" + ticketDateArr[2] + "日 " + ticketDateArr[3] + "时" + ticketDateArr[4] + "分";
		
		var ticketPrice = this.ticketPrice + "元";
		var ticketFlag = this.ticketFlag;
		var ticketState = ticketFlag == "1" ? "已付款" : "已退票";

		var tds = [];
		tds.push("<td >" + ticketCode + "</td>");
		tds.push("<td>" + movieName + "(" + editionName + ")" + "</td>");
		tds.push("<td>" + playTime1 + "</td>");
		tds.push("<td>" + ticketSeat + "</td>");
		tds.push("<td>" + ticketDate + "</td>");
		tds.push("<td>" + ticketPrice + "</td>");
		tds.push("<td>" + ticketState + "</td>");
		tds.push("<td id=\"ticketTime" + ticketId + "\"></td>");
		tds.push("<td id=\"ticketOper" + ticketId + "\"></td>");
		
		var tr = "<tr  style='background-color:white;' id='hd'>" + tds.join("") + "</tr>";
		trs.push(tr);
	});
	
	$("table").append(trs.join(""));

	var page_a = [];
	page_a.push("<a onclick=\"paging('first',1)\">首页</a>");
	page_a.push("<a onclick=\"paging('pre'," + (pageId - 1) +")\">上一页</a>");
	page_a.push("<a onclick=\"paging('next'," + (pageId + 1) +")\">下一页</a>");
	page_a.push("<a onclick=\"paging('last'," + args.pageCount +")\">尾页</a>");
	
	$("#divPage").html(page_a.join("&nbsp;&nbsp;"));
	$("#divTable").show();
	$("#divMsg").hide();

	$(dataArr).each(function(i)
	{
		var ticketId = this.ticketId;
		var ticketCode = this.ticketCode;
		var movieName = this.movieName;
		var playTime1 = this.playTime1;
		var ticketSeat = this.ticketSeat;
		var ticketPrice = this.ticketPrice;
		var ticketFlag = this.ticketFlag;
		var playTime2 = this.playTime2.replaceAll("-", "/");

		var data = {};
		data.ticketId = ticketId;
		data.ticketCode = ticketCode;
		data.movieName = movieName;
		data.playTime1 = playTime1;
		data.ticketSeat = ticketSeat;
		data.ticketPrice = ticketPrice;
		data.ticketFlag = ticketFlag;
		data.playTime2 = playTime2;

		var json = $.toJSON(data);
		setTimeout("showTimeStatus('" + json + "')", 1000);
	});
	var height=$("#divTable").height()+50;
	$("#main_middle").css("height",height);
}

paging = function(type, pageId)
{
	var pageCount = parseInt(args.pageCount);
	
	if(pageId <= 0)
	{
		pageId = 1;
	}
	else if(pageId >= pageCount)
	{
		pageId = pageCount;
	}
	
	args.pageId = pageId;
	
	queryTicket(type);
}

showTimeStatus = function(json)
{
	var obj = $.parseJSON(json);
	
	var ticketId = obj.ticketId;
	var ticketTime = obj.playTime2;
	var ticketFlag = obj.ticketFlag;
	
	var now = new Date();
	var expTime = new Date(ticketTime);

	if(ticketFlag != "1" || now >= expTime)
	{
		$("#ticketTime" + ticketId).html("超时");
		$("#ticketOper" + ticketId).html("已失效");

		return;
	}
	
	var hours = Math.floor((expTime - now) / 1000 / 3600);
	var minutes = Math.floor((expTime - now) / 1000 / 60 % 60);
	var seconds = Math.floor((expTime - now) / 1000 % 60);
	var timeMsg = hours + "小时" + minutes + "分" + seconds + "秒";
	
	$("#ticketTime" + ticketId).html(timeMsg);
	$("#ticketOper" + ticketId).html("<a onclick='backTicket(" + json + ")'>退票</a>");
	
	
	setTimeout("showTimeStatus('" + json + "')", 1000);
}

backTicket = function(obj)
{
	obj.ticketFlag = 0;
	
	var _param = {reqData : obj, reqUrl : "backTicket"};
	
	if(confirm("确定退票吗?"))
	{
		ajaxRequest(_param, function(returnData)
		{
			if(returnData.result)
			{
				alert("退票成功!");
				location.reload();
			}
		}, null);
	}
}

setSelectedCss = function(obj)
{
	$(obj).css("backgroundColor", "#3399FF");
	$(obj).css("color","white");
}

setNotSelectedCss = function(obj)
{
	$(obj).css("backgroundColor", "");
	$(obj).css("color", "#3399FF");
}
function data(){
	$("#leftcolumn ul li").css("background-color","white");
	$("#data").css("background-color","#ccffff");
	$("#ka").html("&nbsp;&gt;&nbsp;编辑资料");
	$("#main_middle").hide();
	$("#divTable").hide();
	$("#changePwd").hide();
	$("#money").hide();
	$("#edit_data").show();
}
function pwd(){
	$("#leftcolumn ul li").css("background-color","white");
	$("#apwd").css("background-color","#ccffff");
	$("#main_middle").hide();
	$("#divTable").hide();
	$("#money").hide();
	$("#edit_data").hide();
	$("#changePwd").show();
	$("#ka").html("&nbsp;&gt;&nbsp;修改密码");
}
function balance(){
	$("#leftcolumn ul li").css("background-color","white");
	$("#balance").css("background-color","#ccffff");
	$("#ka").html("&nbsp;&gt;&nbsp;我的余额");
	$("#main_middle").hide();
	$("#divTable").hide();
	$("#changePwd").hide();
	$("#edit_data").hide();
	$("#money").show();
	var objs = eval("("+json+")");
	$("#num").html(objs.money);
}

